// pages/albumdetail/albumdetail.js

import ArticleAC from '../../js/article_access'
const app = getApp();
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  loadDetail: function (options, cb) {
    console.log('onload detail', options);

    wx.showLoading();

    var albumId = options.id || '4b240523f9ae459c80ba011d9046e5bf';
    this.setData({ albumId: albumId });

    var _this = this;

    ArticleAC.albumDetail({
      albumId : albumId
    }, function(res) {
      console.log('albumDetail', res);
      if (res) {
        var album = res;

        var articles = [];
        if (album) {

          var subs = album.articles;
          if (subs) {
            for (var i in subs) {
              var article = subs[i];
              articles.push({
                id: article.articleId,
                title: article.articleTitle,
                content: article.content,
                articleImg: (article.imageUrl && article.imageUrl + '_200X120' || app.globalData.defaultLogoImg)
              });
            }
          }

          _this.setData({
            title: album.title,
            logo: album.logo + '_140X200',
            info: album.info,
            count: articles.length,
            time: album.createTimeString
          });
        }

        //设置数据
        _this.setData({
          albumArticles: articles
        });
      }
      wx.hideLoading();

      if (cb) {
        cb();
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var temp = app.globalData.statusBar;
    temp.topNavTitle = '专辑详情';
    this.setData(
      {
        barStatus: temp
      }
    );

    this.loadDetail(options);
  },

  onPageScroll: function (e) {
    var top = e.scrollTop || 0;

    var isTitleSet = this.data.isTitleSet || false;
    if (top > 95) {
      if (isTitleSet) {
        return;
      }
      app.setTitle(this, this.data.title);
      this.setData({
        isTitleSet: true
      });
    } else {
      app.setTitle(this, '专辑详情');
      this.setData({
        isTitleSet: false
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadDetail({
      id: this.data.albumId
    }, function () {

      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
  , clickAndHome: function (event) {
    app.clickAndHome(event);
  }

  , clickAndBack: function (event) {
    app.clickAndBack(event);
  },
  /*** 详情跳转 */
  clickListItem: function (event) {

    util.clickListItemAndJump(event);
  }
})